<template>
  <el-dialog v-model="visible"  :width="'30%'" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-row :gutter="22">
        <el-col :span="22">
          <el-form-item label="项目名称" >
            <el-input style="display: none" v-model="dataForm.projectId" ></el-input>
           {{rows.name}}
          </el-form-item>

          <el-form-item label="新项目名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="请输入项目名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";
import {IObject} from "@/types/interface";
const emit = defineEmits(["refreshDataList"]);
const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: '',
  name: '',
  projectId: '',
});
const rows= reactive({
  name: '',
});

const rules = ref({
  name: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
});

const init = (row?: { }) => {
  dataForm.name = "";
  rows.name =row.name
  dataForm.projectId =row.id;
  visible.value = true;

};

// 表单提交
const dataFormSubmitHandle = () => {
  debugger
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
     baseService.get("/sysproject/project/copyProject", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
